<?php

/**
 * alltosun.com 登录 login.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 张元昊 (zhangyh@alltosun.com) $
 * $Date: 2015-1-18 下午2:10:18 $
 * $Id$
*/

class Action
{
    private $user_id;

    public function __construct()
    {
        $this->user_id = user_helper::get_user_id();
    }

    /**
     * 登录页面
     */
    public function __call($action = '', $param = array())
    {
//         $login_action = Request::getParam('action', '');
        $callback = user_helper::get_jump_url();
        if ($this->user_id) {
            $user_info = _uri('user', $this->user_id);
            if(!empty($user_info['uc_id'])) {
                $return_info = user_helper::uc_login($user_info['user_name'], '', $callback);
                echo $return_info;
                return false;
            }
            //如果是邮箱登录
            if(strpos($callback, BBS_URL) === 0){   
                //先判断是否完善过资料
                if(!$user_info['user_name']) {
                    return array('必须先完善资料后才可以在论坛发言。', 'error', AnUrl('openapi/index/bind'));
                } else {
                    //否则再判断是否绑定手机号或者邮箱其一
                    if(!$user_info['uc_id']) {
                        return array('必须先验证邮箱或者手机其一才可以在论坛发言。', 'error', AnUrl('user/setting/account_safety'));
                    }
                }
            }
            return array('您已经登录过了。', 'error', $callback);
            
        }
        $_SESSION['login_callback'] = $callback;
        display_template('login.html');
        // if(is_mobile()) {
        //     Response::display('m/login.html');
        // } else {
        //     Response::display('login.html');
        // }
        
        
    }
    
    /**
     * 登录验证
     */
    public function check_login()
    {
        if ($this->user_id > 0) {
            return array('您已经在登录状态，不用重复登录！', 'error', AnUrl(''));
        }
        $username = trim(Request::Post('username', ''));
        $password = trim(Request::Post('password', ''));
        $logintype = trim(Request::Post('logintype', 'login'));
        $memberpass = trim(Request::Post('memberpass', 0));
        if(isset($_SESSION['login_callback'])) {
            $callback = $_SESSION['login_callback'];
        } else {
            $callback = user_helper::get_jump_url();
        }
        

        if(empty($username)) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'user', 'msg' => '您输入的用户名不能为空');
            }
            // var_dump($username);
            // exit();
            return array('对不起，您输入的用户名不能为空', 'error', AnUrl('user/login'));
        }
        
        if(empty($password)) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'pass', 'msg' => '您输入的密码不能为空');
            }
            return array('对不起，您输入的密码不能为空', 'error', AnUrl('user/login'));
        }

        $get_way = user_helper::get_user_login_way($username);
        switch ($get_way) {
        	case '1':
        	    $filter['email'] = $username;
        	    break;
        	case '2':
        	    $filter['mobile'] = $username;
        	    break;
        
        	default:
        	    $filter['user_name'] = $username;
        	    break;
        }
        
        
        $user_info = _uri('user', $filter);
        if (empty($user_info)) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'pass', 'msg' => '您输入的用户名不存在');
            }
            return array('您输入的用户名不存在', 'error', AnUrl('user/login'));
        }
        // if($get_way == 1 && !$user_info['is_check_email']) {
        //     if(Request::isAjax()) {
        //         return array('info' => 'error', 'type' => 'user', 'msg' => '您输入的邮箱未验证');
        //     }
        //     return array('您输入的邮箱未验证', 'error', AnUrl('user/login'));
        // }
        
        $filter['password'] = user_helper::md5_password($password, $user_info['hash']);
        if ($filter['password'] != $user_info['password']) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'pass', 'msg' => '您输入的账户或者密码错误');
            }
            return array('您输入的账户或者密码错误', 'error', AnUrl('user/login'));
        }
        if ($user_info['status'] == 0) {
            if(Request::isAjax()) {
                return array('info' => 'error', 'type' => 'pass', 'msg' => '登录失败，你的帐户不存在');
            }
            return array('登录失败，你的帐户不存在', 'error', AnUrl('user/login'));
        }
        if(Request::isAjax() && $logintype == 'login') {
            return array('info' => 'success');
        }
        
        if($memberpass) {
            // 记住登录用户的账号和昵称
            user_helper::remember_me_set($user_info, $password);
        }

        // 更新session
        user_helper::session_update($user_info, 'account');


        //更新登录时间
        _model('user')->update($user_info['id'], array('last_login_time' => date('Y-m-d H:i:s', time())));
        
        if(Request::isAjax() && $logintype == 'loginbox') {
            $return_info = user_helper::uc_login($user_info['user_name'], $password, $callback, 1);
        } else {
            $return_info = user_helper::uc_login($user_info['user_name'], $password, $callback);
        }
        
        if($return_info['info'] == 'error') {
            if(Request::isAjax() && $logintype == 'loginbox') {
                return array('info' => 'error', 'type' => 'user', 'msg' => $return_info['msg']);
            }
            return array($return_info['msg'], 'error', AnUrl('user/login'));
        } else {
            if(Request::isAjax() && $logintype == 'loginbox') {
                return $return_info;
            } else {
                // if(ONDEV){
                    echo $return_info;
                // } else {
                //     echo '<pre>';
                //     var_dump($return_info);
                //     echo '</pre>';
                //     exit;
                // }
                
            }
        }
    }

    /*
    * lenovo SSO登录
    */
    public function lenovoSSOlogin()
    {
        $wust = trim(Request::Post('wust', ''));
        $callbackUrl = trim(Request::Post('callbackUrl', ''));
        if($wust) {
            if($this->user_id) {
                return array('info'=>'error');
            } else {
                $return_state = _widget('openapi.lenovo')->create_lenovo_user($wust, 'lenovoSSO', $callbackUrl);
                if($return_state['info'] != 'ok') {
                    return array('info'=>'error');
                } else {
                    return array('info'=>'ok', 'msg'=>$return_state['msg']);
                }
            }
        } else {
            return array('info'=>'error');
        }
    }

}
?>